import {fetchSearchListAPI} from "@/api/searchApi";
import {getHistoryList, removeHistoryList, setHistoryList} from "@/utils/authHistoryList";

const actions = {
    /* 获取搜索建议列表的action */
    async fetchSearchList(context, keyword) {
        const {message: searchList} = await fetchSearchListAPI(keyword)
        context.commit("updateSearchList", searchList)
    }
}
const mutations = {
    /* 更新搜索建议列表的mutation */
    updateSearchList(state, searchList) {
        state.searchList = searchList
    },
    /* 往搜索历史列表里push一个元素的mutation */
    unshiftHistoryList(state, keyword) {
        state.historyList.unshift(keyword)
        setHistoryList(state.historyList)
    },
    /* 清空搜索历史列表的mutation */
    clearHistoryList(state) {
        state.historyList = []
        removeHistoryList()
    }
}
const state = () => {
    return {
        /* 搜索的结果列表 */
        searchList: [],
        /* 搜索的历史列表 */
        historyList: getHistoryList()
    }
}

const getters = {}
export default {
    namespaced: true,
    actions,
    mutations,
    state,
    getters
}
